/**
 *FileUtils.java
 *@anthor Daniel Liu
 *2006-4-28 8:04:33
 **/
package org.lex.io.filesystem;

import java.io.File;

//use SizeFormat instead
@Deprecated
class FileSizeUtilities {
	public static final String B = " B";
	public static final String KB = " KB";
	public static final String MB = " MB";
	public static final String GB = " GB";
	public static final String AUTO = "auto";

	private FileSizeUtilities() {
	}

	/**
	 * ��ʽ���ļ���Ϣ�ķ��� ����File.length()���ص���long �������ṩ�ķ������Ը�ʽ����Щ���
	 */
	public static String formatFileSize(long size) {
		if (size <= 1024L)
			return size + B;
		float temp = size / 1024f;
		if (temp <= 1024f)
			return Math.round(temp * 10) / 10f + KB;
		temp /= 1024f;
		if (temp <= 1024f)
			return Math.round(temp * 100) / 100f + MB;
		temp /= 1024f;
		return Math.round(temp * 1000) / 1000f + GB;
	}

	public static String formatFileSize(long size, String unit) {
		if (unit == FileSizeUtilities.B)
			return size + FileSizeUtilities.B;
		if (unit == FileSizeUtilities.KB)
			return size / 1024L + FileSizeUtilities.KB;
		if (unit == FileSizeUtilities.MB)
			return size / 1048576L + FileSizeUtilities.MB;
		if (unit == FileSizeUtilities.GB)
			return size / 1073741824L + FileSizeUtilities.GB;
		// auto-mode
		return FileSizeUtilities.formatFileSize(size);
	}

	public static String formatFileSize(File file, String unit) {
		if (null == file || false == file.isFile())
			return "";
		return FileSizeUtilities.formatFileSize(file.length(), unit);
	}

	public static String formatFileSize(File file) {
		return FileSizeUtilities.formatFileSize(file, FileSizeUtilities.AUTO);
	}
}
